
$(document).ready(function(){  

	
	var arr_files = {};
    $("#fileupload").bind("fileuploaddone", function (e, data) {
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
		
    	$.each(data.result.files, function (index, file) {
    		if(file.url.indexOf('/pinnwand/') != -1){
    			// get ordner id
    			var arr_file = file.url.split('/');
    			var ordner = "";
    			$.each(arr_file, function(i, val){
    				if(i >= (arr_file.length - 5)){
    					if(ordner != ''){
    						ordner += '---___---' + decodeURIComponent(val);	
    					} else {
    						ordner = decodeURIComponent(val);
    					}
    						
    				}
    			});
    			delete_filename = "";
    			var temp_delete_filename = ordner;
    			temp_delete_filename = temp_delete_filename.split('---___---');
    			
    			$.each( temp_delete_filename, function (i, val) {
    				if(i < temp_delete_filename.length - 1 ){
    					
    					if(delete_filename != ''){
    						delete_filename += val + '---___---';
    					} else {
    						delete_filename = '---___---' + val + '---___---';
    					}
    				}
    			});
    			
    			url = protocol + '//' + host + $( "input[name='url-prepare-images']").val() + '/' + ordner;
    			$.post( url, { text: $( "textarea[name='pinnwand-upload']").val()  } );
    			
    		} else {
        		if(arr_files[file.name] == undefined){
        			$("#personal-thumb").append('<img src="/personal/' + username + '/files/thumbnail/' + file.name + '" style="padding-right:2px;">');	
        			// set standart
        			url = protocol + '//' + host + '/auth/security/updatepersonal/n_ewfile';
        			$.get( url );
        		}
    		}
    	});    		
    });    
    
    $('#fileupload').bind('fileuploaddestroy', function (e, data) {
    	
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
    	
    	
		var file = decodeURIComponent(data.url.substring(data.url.indexOf("=") + 1));
		if(data.url.indexOf('/pinnwand-') != -1){
			var url = decodeURIComponent(data.url);
    		url = url.split('/');
    		url = url[url.length - 1];
    		url = url.split('?');
    		var path = url[0].split('-');
    		var filename = '';
    		$.each(path, function(i, val){
   				filename += '---___---' + val;
    		});
    		filename += '---___---';
    		delete_filename = filename;
    		url = protocol + '//' + host + $( "input[name='url-check-directory']").val() + '/' + filename;
    		// prüfe ob noch files im ordner vorhanden sind
    		setTimeout( function(){
    			jQuery.get( url );	
    		}, 1000);
    		
    		
		} else {
			$("#personal-thumb").children().each(function(){
				if($(this).attr("src") == '/personal/' + username + '/files/thumbnail/' + file){
					$(this).remove();
				}
			});
		}
	
		
    });
    
	 $(document).on("click", "#personal-thumb img", 
		  function(){
		 	 
		 	 var obj = $(this); 
	    	 var arr_filename = $(this).attr("src").split('/');
	    	 var filename = "";
	    	 $.each(arr_filename, function(i,val){
	    		 filename = val;
	    	 });
	    	 var pathArray = window.location.href.split('/');
	    	 protocol = pathArray[0];
	    	 host = pathArray[2];
	    	 url = protocol + '//' + host + '/auth/security/updatepersonal/' + filename;
	    	 $("#aj-set-image-thumb").fadeIn();
	    	 $.get( url , function(data){
	    		 $("#aj-set-image-thumb").fadeOut();

	    		 $.each($("#personal-thumb img"), function(i,val){
				 		$(this).css({"border":"0px"});
				 	 });
	    		 obj.css({"border":"2px solid red"});
	    		 
	    		 $("#personal-image-profil").attr("src",obj.attr("src").replace('/thumbnail/','/profilbild/'));
	    		 
	    	 });
	  });
    

	    $("#submit-pinnwand-images").click(function(e){
	    	e.preventDefault();
	    	$("#profilbild-area").hide();
	    	$("#pinnwand-area").show();
	    	$( "textarea[name='pinnwand-upload']").val("");
	    	$("#aj-set-image-bildupload").hide();
	    	
	    	// prüfe welches images directory
	    	
			var pathArray = window.location.href.split('/');
			protocol = pathArray[0];
			host = pathArray[2];
			url = protocol + '//' + host + $( "input[name='url-pinnwand-images']").val();
			var fileupload = "";
			$.get(url, function(data){
				
				//url = protocol + '//' + host + $( "input[name='url-pinnwand-images']").val();	
				//url_fileupload = 'uploadimages/pinnwand';
				url_fileupload = 'uploadimages/' + data;
				
				$.getScript("/bundles/acmeperson/js/main.js", function(){
			    	var width = parseInt($(".container").width());
					var marginleft = width / 2;
					
					var height = $("body").height() + 50;	
					$("#pers-bild-lightbox").css( { "height":height + "px" } );
					$("#pers-bild-lightbox-panel").width(width);
					$("#pers-bild-lightbox-panel").css({"margin-left" :  "-" + marginleft + "px"});
					
					$("#pers-bild-lightbox, #pers-bild-lightbox-panel").fadeIn(300);  
					
				});
			});
	    });

    
	    function getHtml(){
	    	
	    }

     
    
});  